package benny_driving_ser.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import benny_driving_ser.dao.PersonInfoDao;
import benny_driving_ser.entity.DriPerInfo;
import benny_driving_ser.util.DBUtil;

public class PersonInfoDaoImpl implements PersonInfoDao {
	private PersonInfoDaoImpl() {

	}

	private static class PersonInfoDaoImplHolder {
		private static PersonInfoDaoImpl instance = new PersonInfoDaoImpl();
	}

	public static PersonInfoDaoImpl getInstance() {
		return PersonInfoDaoImplHolder.instance;
	}

	public DriPerInfo getDriPerInfo(int driid) {
		String sql_query = "SELECT t_user.username,t_user.pic,t_user.xm,t_user.dh,t_user.sex,t_zjlx.mc,t_user.sfzh,t_user.jszh,t_user.cadd,t_user.dz,t_user.jjlxr,t_user.jjlxrxm,t_banksj.kh FROM t_user INNER JOIN t_banksj ON t_user.id = t_banksj.hyid INNER JOIN t_zjlx ON t_user.zjlxid = t_zjlx.id WHERE t_user.id = "
				+ driid;
		Connection con = DBUtil.getInstance().openConnection();
		try {
			con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
			Statement s = con.createStatement();
			ResultSet rs = s.executeQuery(sql_query);
			if (rs.next()) {
				DriPerInfo dpi = new DriPerInfo(rs.getString(1),
						rs.getString(2), rs.getString(3), rs.getString(4),
						rs.getString(5), rs.getString(6), rs.getString(7),
						rs.getString(8), rs.getString(9), rs.getString(10),
						rs.getString(11), rs.getString(12), rs.getString(13));
				DBUtil.getInstance().closeConnection(con);
				return dpi;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DBUtil.getInstance().closeConnection(con);
		return null;
	}

	public List<DriPerInfo> getDriList(List<Integer> ids) {
		String sql = "SELECT t_user.xm,t_user.id,t_user.sfzh,t_user.dh,t_user.stars,t_user.pic,t_user.tjbz FROM t_user WHERE id = ?";
		List<DriPerInfo> dpis = new ArrayList<DriPerInfo>();
		Connection con = DBUtil.getInstance().openConnection();
		try {
			con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
			for (Integer i : ids) {
				PreparedStatement ps = con.prepareStatement(sql);
				ps.setInt(1, i);
				ResultSet rs = ps.executeQuery();
				if (rs.next()) {
					dpis.add(new DriPerInfo(rs.getString(2), rs.getString(6)
							+ "", rs.getString(1) + "", rs.getString(4), rs
							.getString(3), rs.getInt(5) + "", rs.getString(7)));
				}
			}

			DBUtil.getInstance().closeConnection(con);
			return dpis;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DBUtil.getInstance().closeConnection(con);
		return null;
	}

}
